package com.launch.carmanager.common.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.View;


import com.launch.carmanager.R;
import com.launch.carmanager.common.widget.TitleBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import icepick.Icepick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by ganquan on 2018/7/12 17:32
 */
public class BaseFragment<P extends BasePresenter> extends Fragment
        implements BaseView, TitleBar.OnClickListener {

    private Unbinder unbinder;

    protected P presenter;

    protected TitleBar mTitleBar;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTitleBar = find(R.id.title_bar);
        if (mTitleBar != null) {
            mTitleBar.setOnClickListener(this);
        }

        unbinder = ButterKnife.bind(this, view);

        Icepick.restoreInstanceState(this, savedInstanceState);
        onOthersInit();

        presenter = newPresenter();
        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }

    protected <T extends View> T find(int id) {
        if (getView() == null) {
            return null;
        }
        return getView().findViewById(id);
    }


    /**
     * 其他初始化
     */
    protected void onOthersInit() {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    public boolean hasPermission(@NonNull String... permissions) {
        return EasyPermissions.hasPermissions(getContext(), permissions);
    }

    public void requestPermission(@NonNull String rationale, int requestCode, @NonNull String... perms) {
        EasyPermissions.requestPermissions(this, rationale, requestCode, perms);
    }

    public void requestPermission(@StringRes int rationale, int requestCode, @NonNull String... perms) {
        requestPermission(getString(rationale), requestCode, perms);
    }


    protected P newPresenter() {
        return null;
    }


    @Override
    public void alert(CharSequence msg) {
        BaseActivity parent = (BaseActivity) getActivity();
        parent.alert(msg);
    }

    @Override
    public void alert(int msg) {
        alert(getString(msg));
    }

    @Override
    public void showProgressDialog(CharSequence msg) {

    }

    @Override
    public void dismissProgressDialog() {

    }

    @Override
    public void setProgressMessage(CharSequence msg) {

    }

    @Override
    public void closePage() {

    }

    @Override
    public void onLeftClick() {
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onTitleClick() {

    }

    @Override
    public void onRightLeftClick() {

    }

    protected boolean useEventBus() {
        return false;
    }

    protected void openPage(Class<? extends Activity> clazz) {
        BaseActivity parent = (BaseActivity) getActivity();
        parent.openPage(clazz);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        unbinder.unbind();
    }
}
